<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    :width="width"
    :before-close="handleClose"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="auto"
      :label-position="labelPosition"
    >
      <template v-for="item in formItems" :key="item.prop">
        <el-form-item :label="item.label" :prop="item.prop" v-if="!item.hidden">
          <component
            :is="getComponentType(item.type)"
            v-model="formData[item.prop]"
            v-bind="item.attrs || {}"
            v-on="item.listeners || {}"
          >
            <template v-if="item.type === 'select' && item.options">
              <el-option
                v-for="opt in item.options"
                :key="opt.value"
                :disabled="opt.disabled"
                :label="opt.label"
                :value="opt.value"
              />
            </template>

            <template v-if="item.type ==='upload'">
              <div>3333</div>
            </template>
          </component>
        </el-form-item>
      </template>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch, nextTick } from 'vue'
const getComponentType = (type) => {
  const componentMap = {
    input: 'el-input',
    select: 'el-select',
    date: 'el-date-picker',
    daterange: 'el-date-picker',
    datetime: 'el-date-picker',
    datetimerange: 'el-date-picker',
    switch: 'el-switch',
    upload: 'ImageUpload'
  }
  return componentMap[type] || 'el-input'
}
const props = defineProps({
  // 弹窗是否显示
  visible: {
    type: Boolean,
    default: false
  },
  // 弹窗标题
  title: {
    type: String,
    default: '表单'
  },
  // 弹窗宽度
  width: {
    type: String,
    default: '50%'
  },
  // 表单配置项
  formItems: {
    type: Array,
    default: () => []
  },
  // 表单数据
  formData: {
    type: Object,
    default: () => ({})
  },
  // 表单验证规则
  formRules: {
    type: Object,
    default: () => ({})
  },
  // 标签位置
  labelPosition: {
    type: String,
    default: 'right'
  },
  // 是否正在提交
  loading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:visible', 'submit', 'close'])

const dialogVisible = ref(false)
const formRef = ref(null)

// 监听visible变化
watch(
  () => props.visible,
  (val) => {
    dialogVisible.value = val
    if (val) {
      nextTick(() => {
        formRef.value?.clearValidate()
      })
    }
  }
)

// 监听dialogVisible变化
watch(dialogVisible, (val) => {
  emit('update:visible', val)
})

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false
  emit('close')
}

// 提交表单
const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    emit('submit', props.formData)
  } catch (error) {
    console.log('表单验证失败', error)
  }
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>